package com.daisyday.tank;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TankFrame extends Frame {

    Tank mytank = new Tank(50, 50, Dir.DOWN);

    public TankFrame() {
        this.setSize(800, 600);
        this.setResizable(false);
        this.setTitle("tank game");
        this.setVisible(true);
        this.addKeyListener(new MyKeyListener());

        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });


    }

    Image offScreamImage=null;
    @Override
    public void update(Graphics g){
        if(offScreamImage==null){
            offScreamImage=this.createImage(800,600);
        }
        Graphics graphics = offScreamImage.getGraphics();
        Color color = graphics.getColor();
        graphics.setColor(Color.black);
        graphics.fillRect(0,0,800,600);
        graphics.setColor(color);
        paint(graphics);
        g.drawImage(offScreamImage,0,0,null);


    }



    @Override
    public void paint(Graphics g) {
        mytank.paint(g);

    }

    class MyKeyListener extends KeyAdapter {

        boolean bL = false;
        boolean bU = false;
        boolean bR = false;
        boolean bD = false;


        @Override
        public void keyPressed(KeyEvent e) {
            int key = e.getKeyCode();
            switch (key) {
                case KeyEvent.VK_LEFT:
                    bL = true;
                    break;
                case KeyEvent.VK_UP:
                    bU = true;
                    break;
                case KeyEvent.VK_RIGHT:
                    bR = true;
                    break;
                case KeyEvent.VK_DOWN:
                    bD = true;
                    break;
                default:
                    break;
            }
            setMainTankDir();

        }

        @Override
        public void keyReleased(KeyEvent e) {
            int key = e.getKeyCode();
            switch (key) {
                case KeyEvent.VK_LEFT:
                    bL = false;
                    break;
                case KeyEvent.VK_UP:
                    bU = false;
                    break;
                case KeyEvent.VK_RIGHT:
                    bR = false;
                    break;
                case KeyEvent.VK_DOWN:
                    bD = false;
                    break;
                default:
                    break;
            }
            setMainTankDir();
        }

        private void setMainTankDir() {
            mytank.setMoving(true);
            if (bL) mytank.setDir(Dir.LEFT);
            if (bU) mytank.setDir(Dir.UP);
            if (bR) mytank.setDir(Dir.RIGHT);
            if (bD) mytank.setDir(Dir.DOWN);

            if(!bL && !bU && !bR && !bD){
                mytank.setMoving(false);
            }
        }
    }
}
